package com.dq.domain.sys.user.user;

import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.domain.sys.user.user.centre.UserCard;
import com.dq.exception.customException.AdminException;
import com.dq.vo.baseVo.VO;

import com.dq.domain.sys.menu.SysMenu;
import com.dq.domain.org.Org;
import com.dq.domain.org.OrgCustomerLevel;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.sys.user.UserHobby;
import com.dq.domain.sys.user.user.base.FrontUser;
import com.dq.utils.AgeUtils;
import com.dq.utils.EditCheckUtil;
import com.dq.utils.TimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.*;

import static com.dq.vo.baseVo.VO.inCaseNull;

/**
 * @创建人 gewenzhen
 * @创建时间 2019/10/21
 * @描述
 */
@Data
@Entity
@ApiModel("微信用户信息")
public class WxUser extends FrontUser {
    @ApiModelProperty("开卡时间, 添加会员卡号时")
    protected Long openCardTime;
    @ApiModelProperty(value = "会员卡数量")
    private Long cardCount;
    @ApiModelProperty("最后服务时间")
    protected Long serveTime;
    @ApiModelProperty("最后服务员工集合, 逗号分隔")
    protected String serveUserIdList;
    protected String serveUserNameList;
    @ApiModelProperty(value = "消费总金额")
    private BigDecimal payTotal;
    @ApiModelProperty("最后消费时间")
    protected Long payTime;
    @ApiModelProperty("最后充值时间")
    protected Long topTime;
    @ApiModelProperty(value = "用户openId",hidden = true)
    @Column(unique = true)
    protected String openid;
    @ApiModelProperty(value = "用户unionid，用来区分用户",hidden = true)
    protected String unionid;

    @ManyToOne
    @ApiModelProperty("用户等级")
    protected OrgCustomerLevel orgCustomerLevel;

    @ApiModelProperty("用户类型,默认为活跃")
    @Enumerated(EnumType.ORDINAL)
    protected CustomerType customerType;

    @ApiModelProperty("跟踪记录生成时间")
    protected Long generateTime;

    @ApiModelProperty("爱好")
    protected String hobby;

    @ApiModelProperty("父类用户")
    protected Long parentId;

    @Column(columnDefinition="text")
    @ApiModelProperty("皮肤档案")
    public String skinJson;

    @ApiModelProperty("客户类型")
    @Enumerated(EnumType.ORDINAL)
    protected MemberType memberType;

    @ApiModelProperty("备注")
    @Column(columnDefinition="text")
    protected String remark;

    @ApiModelProperty("核销二维码")
    @Column(columnDefinition="text")
    protected String checkoutImg;

    @Transient
    protected Long integral;
    @Transient
    @ApiModelProperty("标签对象ID集合")
    protected Set<Long> lableIdList = new HashSet<>();
    @Transient
    protected List<Long> lableIdCuntList = new ArrayList<>();
    @Transient
    @ApiModelProperty("所属门店ID, 不传根据登录账号关联门店")
    protected Long orgId;
    /**
     * 每月核算用户类型
     */
    public enum CustomerType{
        active("活跃客户"),silent("沉默用户");
        public String name;

        CustomerType(String name){
            this.name = name;
        }
    }
    public enum MemberType{
        experience("新顾客"),formal("老顾客");
        public String name;

        MemberType(String name){
            this.name = name;
        }
    }

    public WxUser () {
    }
    public WxUser(SysSection section, String healImg, String realCName, String phone, String birthday, String idCardNo, String wxNumber, String employerName,
                  OrgCustomerLevel orgCustomerLevel, Gender gender, Long enterTime, String hobby)throws AdminException {
        this.section = section;
        this.headImg = healImg;
        this.userRealcName = realCName;
        this.mobile = phone;
        this.username = phone;
        this.brithday = TimeUtil.getTime(birthday);
        this.idCardNo = idCardNo;
        this.wxNumber = wxNumber;
        this.employerName = employerName;
        this.orgCustomerLevel = orgCustomerLevel;
        this.gender = gender;
        this.cardNo = generyStrAndMstime("HM");
        this.inviteTime = System.currentTimeMillis();
        this.entryTime = enterTime;
        this.shareCode = EditCheckUtil.generateShortUuid();
        this.hobby = hobby;
    }
    public WxUser(SysSection section, String healImg, String realCName, String phone, String email, Long birthdayLong, String idCardNo, String wxNumber, String employerName,
                  OrgCustomerLevel orgCustomerLevel, Gender gender, Long enterTime,String hobby)throws AdminException {
        this.section = section;
        this.headImg = healImg;
        this.userRealcName = realCName;
        this.mobile = phone;
        this.email = email;
        this.username = phone;
        this.brithday = birthdayLong;
        this.idCardNo = idCardNo;
        this.wxNumber = wxNumber;
        this.employerName = employerName;
        this.orgCustomerLevel = orgCustomerLevel;
        this.gender = gender;
        this.cardNo = generyStrAndMstime("HM");
        this.inviteTime = System.currentTimeMillis();
        this.entryTime = enterTime == null ? System.currentTimeMillis() : enterTime;
        this.shareCode = EditCheckUtil.generateShortUuid();
        this.hobby = hobby;
    }

    public WxUser(String healImg,String realCName,String phone,String birthday,String idCardNo,String wxNumber,String employerName,
                  OrgCustomerLevel orgCustomerLevel,Gender gender,String hobby)throws AdminException {
        this.headImg = healImg;
        this.userRealcName = realCName;
        this.mobile = phone;
        this.username = phone;
        this.brithday = TimeUtil.getTime(birthday);
        this.idCardNo = idCardNo;
        this.wxNumber = wxNumber;
        this.employerName = employerName;
        this.orgCustomerLevel = orgCustomerLevel;
        this.gender = gender;
        this.hobby = hobby;
    }


    @Data
    @ApiModel(value = "用户视图")
    public static class Vo implements VO {
        protected Long id;
        @ApiModelProperty("真实姓名")
        protected String userRealCName;
        @ApiModelProperty("顾客卡号/工号, 区别会员卡号")
        protected String cardNo;
        @ApiModelProperty("头像")
        protected String headImg;
        /** todo 需赋值 */
        @ApiModelProperty("标签(名称+数目拼接)")
        protected List<String> lableStrList;
        protected List<Map<String, Object>> lableMapList;
        /** todo 需赋值 */
        @ApiModelProperty("会员卡对象集合")
        protected List<UserCard.Vo> userCardList;

        @ApiModelProperty("开卡时间, 添加会员卡号时")
        protected String openCardTime;
        @ApiModelProperty(value = "会员卡数量")
        private Long cardCount;
        @ApiModelProperty("最后服务时间")
        protected String serveTime;
        @ApiModelProperty("最后服务员工集合, 逗号分隔")
        protected String serveUserIdList;
        protected String serveUserNameList;

        @ApiModelProperty("手机号")
        protected String mobile;
        @ApiModelProperty(value = "性别")
        private String genderCn;
        private String genderEn;
        @ApiModelProperty(value = "状态")
        private String statusCn;
        private String statusEn;

        @ApiModelProperty(value = "所属店铺")
        private Long orgId;
        private String orgName;
        @ApiModelProperty(value = "消费总金额")
        private BigDecimal payTotal;
        @ApiModelProperty("最后消费时间")
        protected String payTime;
        @ApiModelProperty("最后充值时间")
        protected String topTime;

        @ApiModelProperty(value = "创建时间")
        private String createTime;
        @ApiModelProperty("备注")
        protected String remark;

        @ApiModelProperty(value = "用户openId")
        private String openid;
        @ApiModelProperty(value = "用户unionid，用来区分用户")
        private String unionid;

        private String token;

        private Integer payCount;
    }
    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id = this.getId();
        vo.userRealCName = fmt(this.getUserRealcName());
        vo.cardNo = fmt(this.getCardNo());
        vo.headImg = fmt(this.getHeadImg());
        vo.openCardTime = fmt(this.getOpenCardTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.cardCount = fmt(this.getCardCount());
        vo.serveTime = fmt(this.getServeTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.serveUserIdList = this.getServeUserIdList();
        vo.serveUserNameList = this.getServeUserNameList();
        vo.mobile = fmt(this.getMobile());

        vo.genderCn = this.getGender() == null ? Gender.xx.name : this.getGender().name;
        vo.genderEn = this.getGender() == null ? Gender.xx.toString() : this.getGender().toString();
        vo.statusCn = this.getStatus() == null ? "" : this.getStatus().name;
        vo.statusEn = this.getStatus() == null ? "" : this.getStatus().toString();
        vo.orgId = this.getSection() == null ? null : this.getSection().getId();
        vo.orgName = this.getSection() == null ? "" : this.getSection().getSimpleName();
        vo.payTotal = fmt(this.getPayTotal());
        vo.payTime = fmt(this.getPayTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.topTime = fmt(this.getTopTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.createTime = fmt(this.getCreateTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.remark = fmt(this.getRemark());

        vo.openid = fmt(this.getOpenid());
        vo.unionid = fmt(this.getUnionid());

        return vo;
    }

    @Data
    public static class FrontWxUser{
        protected Long id;
        @ApiModelProperty("真实姓名")
        protected String userRealCName;
        @ApiModelProperty("顾客卡号/工号, 区别会员卡号")
        protected String cardNo;
        @ApiModelProperty("头像")
        protected String headImg;
        /** todo 需赋值 */
        @ApiModelProperty("标签(名称+数目拼接)")
        protected List<String> lableStrList;
        /** todo 需赋值 */
        @ApiModelProperty("会员卡对象集合")
        protected List<UserCard.Vo> userCardList;

        @ApiModelProperty("开卡时间, 添加会员卡号时")
        protected String openCardTime;
        @ApiModelProperty(value = "会员卡数量")
        private Long cardCount;
        @ApiModelProperty("最后服务时间")
        protected String serveTime;
        @ApiModelProperty("最后服务员工集合, 逗号分隔")
        protected String serveUserIdList;
        protected String serveUserNameList;

        @ApiModelProperty("手机号")
        protected String mobile;
        @ApiModelProperty(value = "性别")
        private String genderCn;
        private String genderEn;
        @ApiModelProperty(value = "状态")
        private String statusCn;
        private String statusEn;

        @ApiModelProperty(value = "所属店铺")
        private Long orgId;
        private String orgName;
        @ApiModelProperty(value = "消费总金额")
        private BigDecimal payTotal;
        @ApiModelProperty("最后消费时间")
        protected String payTime;
        @ApiModelProperty("最后充值时间")
        protected String topTime;

        @ApiModelProperty(value = "创建时间")
        private String createTime;
        @ApiModelProperty("备注")
        protected String remark;
    }
    public FrontWxUser toFrontWxUser() {
        FrontWxUser vo = new FrontWxUser();

        vo.id = fmt(this.getId());
        vo.userRealCName = fmt(this.getUserRealcName());
        vo.cardNo = fmt(this.getCardNo());
        vo.headImg = fmt(this.getHeadImg());
        vo.openCardTime = fmt(this.getOpenCardTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.cardCount = fmt(this.getCardCount());
        vo.serveTime = fmt(this.getServeTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.serveUserIdList = this.getServeUserIdList();
        vo.serveUserNameList = this.getServeUserNameList();
        vo.mobile = fmt(this.getMobile());

        vo.genderCn = this.getGender() == null ? Gender.xx.name : this.getGender().name;
        vo.genderEn = this.getGender() == null ? Gender.xx.toString() : this.getGender().toString();
        vo.statusCn = this.getStatus() == null ? "" : this.getStatus().name;
        vo.statusEn = this.getStatus() == null ? "" : this.getStatus().toString();
        vo.orgId = this.getSection() == null ? null : this.getSection().getId();
        vo.orgName = this.getSection() == null ? "" : this.getSection().getSimpleName();
        vo.payTotal = fmt(this.getPayTotal());
        vo.payTime = fmt(this.getPayTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.topTime = fmt(this.getTopTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.createTime = fmt(this.getCreateTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);

        vo.remark = fmt(this.getRemark());

        return vo;
    }



    @ApiModel("用户选择框")
    @Data
    public static class SimpleWxUser{
        @ApiModelProperty("用户id")
        protected Long wxUserId;
        @ApiModelProperty("会员卡号/工号")
        protected String cardNo;
        @ApiModelProperty("用户真实姓名")
        protected String userRealCName;
        @ApiModelProperty("手机号")
        protected String username;
        @ApiModelProperty("所属门店")
        protected String ofOrgName;
        @ApiModelProperty("会员等级")
        protected String level;
    }

    public SimpleWxUser toSimpleWxUser(String ofOrgName) {
        SimpleWxUser vo = new SimpleWxUser();
        vo.wxUserId = this.id;
        vo.cardNo = this.cardNo;
        vo.username = this.username;
        vo.userRealCName = this.userRealcName;
        vo.ofOrgName = ofOrgName;
        vo.level = this.orgCustomerLevel != null ? this.orgCustomerLevel.getLevenName() : "";
        return vo;
    }

    //重写hashCode，更改判断对象相同的策略
    @Override
    public int hashCode() {
        String result = username;
        return result.hashCode();
    }
    //修改equal  更改判断对象相等的策略
//    @Override
//    public boolean equals(Object obj) {
//        WxUser u = (WxUser) obj;
//        return this.getUsername().equals(u.getUsername());
//    }

}
